/**
******************************************************************************
* 文件名程: adc.c 
* 作    者: 东莞-邓凯歌 微信:D13631760681
* 功    能: ADC配置
* 硬    件: PY32F403C_KFB_V0.1
* 说明：
* 软件版本 V1.0.1(2024/5/6 16:02)
******************************************************************************
**/
#include "adc.h"



ADC_HandleTypeDef hadc1;
DMA_HandleTypeDef hdma_adc1;

uint16_t ADC1_Datas[128];

void APP_ADC1_Init(void)
{

	ADC_ChannelConfTypeDef sConfig = {0};

	RCC_PeriphCLKInitTypeDef RCC_PeriphCLKInit;
	RCC_PeriphCLKInit.PeriphClockSelection= RCC_PERIPHCLK_ADC;
	RCC_PeriphCLKInit.AdcClockSelection   = RCC_ADCPCLK2_DIV8;
	HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphCLKInit);

	/** Common config
	*/
	hadc1.Instance = ADC1;
	hadc1.Init.Resolution            = ADC_RESOLUTION_12B;             /* 分辨率12位 */
	hadc1.Init.DataAlign             = ADC_DATAALIGN_RIGHT;            /* 对齐方式右对齐 */
	hadc1.Init.ScanConvMode          = ADC_SCAN_ENABLE;                /* 扫描方式关闭 */
	hadc1.Init.ContinuousConvMode    = ENABLE;                        /* 单次模式 */
	hadc1.Init.NbrOfConversion       = 1;                              /* 转换通道数4 */
	hadc1.Init.DiscontinuousConvMode = DISABLE;                        /* 间断模式不使能 */
	hadc1.Init.NbrOfDiscConversion   = 1;                              /* 间断模式短序列长度为1 */
	hadc1.Init.ExternalTrigConv      = ADC_SOFTWARE_START;             /* 软件触发 */
  
  
	if (HAL_ADC_Init(&hadc1) != HAL_OK)
	{
		Error_Handler();
	}

	/** Configure Regular Channel
	*/
	sConfig.Channel = ADC_CHANNEL_0;
	sConfig.Rank = ADC_REGULAR_RANK_1;
	sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
	if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
	{
		Error_Handler();
	}

	/* ADC Calibrate */    
	if (HAL_ADCEx_Calibration_Start(&hadc1) != HAL_OK)
	{
		Error_Handler();
	}
	/* ADC开启,并使能DMA */
	if (HAL_ADC_Start_DMA(&hadc1, (uint32_t *)ADC1_Datas, 128) != HAL_OK)            
	{
		Error_Handler();
	}

}

void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
{

	GPIO_InitTypeDef GPIO_InitStruct = {0};
	if(adcHandle->Instance==ADC1)
	{
		/* ADC1 clock enable */
		__HAL_RCC_ADC1_CLK_ENABLE();

		__HAL_RCC_GPIOA_CLK_ENABLE();
		/**ADC1 GPIO Configuration
		PA0-WKUP     ------> ADC1_IN0
		*/
		GPIO_InitStruct.Pin = GPIO_PIN_0;
		GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
		HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

		/* ADC1 DMA Init */
		/* ADC1 Init */
		hdma_adc1.Instance = DMA1_Channel1;
		hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY;
		hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE;
		hdma_adc1.Init.MemInc = DMA_MINC_ENABLE;
		hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
		hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
		hdma_adc1.Init.Mode = DMA_CIRCULAR;
		hdma_adc1.Init.Priority = DMA_PRIORITY_MEDIUM;
		if (HAL_DMA_Init(&hdma_adc1) != HAL_OK)
		{
			Error_Handler();
		}

		__HAL_LINKDMA(adcHandle,DMA_Handle,hdma_adc1);
		HAL_DMA_ChannelMap(&hdma_adc1,DMA_CHANNEL_MAP_ADC1);
	}
}

void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle)
{

	if(adcHandle->Instance==ADC1)
	{
		/* Peripheral clock disable */
		__HAL_RCC_ADC1_CLK_DISABLE();

		/**ADC1 GPIO Configuration
		PA0-WKUP     ------> ADC1_IN0
		*/
		HAL_GPIO_DeInit(GPIOA, GPIO_PIN_0);

		/* ADC1 DMA DeInit */
		HAL_DMA_DeInit(adcHandle->DMA_Handle);
	}
}

